package br.com.toscano.contpedido.persistence;

import java.util.List;

import javax.persistence.Query;

import br.com.toscano.contpedido.domain.Cliente;
import br.gov.frameworkdemoiselle.stereotype.PersistenceController;
import br.gov.frameworkdemoiselle.template.JPACrud;

@PersistenceController
public class ClienteDAO extends JPACrud<Cliente, Long> {

	private static final long serialVersionUID = 1L;

	public List<Cliente> pesquisarCliente(String telefone, String nome) {

		String hql = "from "+Cliente.class.getSimpleName()+" obj ";
		boolean jaTemRestricao = false;
		if (telefone !=  null && !"".equalsIgnoreCase(telefone) ) {
			if(jaTemRestricao) {
				hql = hql + " and ";
			} else {
				hql = hql + " where ";
			}
			hql = hql + " obj.fone like '%" + telefone +"%' or  obj.fone2 like '%" + telefone +"%'  or  obj.fone3 like '%" + telefone +"%' ";
			jaTemRestricao = true;
		}

		if (nome !=  null  && !"".equalsIgnoreCase(nome) ) {
			if(jaTemRestricao) {
				hql = hql + " and ";
			} else {
				hql = hql + " where ";
			}
			hql = hql + " obj.nome like '%" + nome +"%' ";
			jaTemRestricao = true;
		}

		hql = hql + " order by obj.nome desc ";

		Query q = createQuery(hql);
//		if (telefone !=  null ) {
//			q.setParameter("telefone", telefone);
//		}

		List<Cliente> lista = q.getResultList();
		return lista;
	}
}
